;String.prototype.trim=function(){return this.replace(/(^\s+)|(\s+$)/g,'')};
;(function(win,doc){
    var ie=false;
    if(!doc.querySelector && doc.createStyleSheet){
        var ies=doc.createStyleSheet(),ie=true;
    }
    function _ie(r,v,d){
        var a=d.all, c=[], r = r.replace(/\[for\b/gi, '[htmlFor').split(','),i,j;
        for(i=r.length; i--;){
            ies.addRule(r[i], 'k:v');
            for(j=a.length; j--;){
                a[j].currentStyle.k && c.push(a[j]);
            }
            ies.removeRule(0);
        }
        return v?c:c[c.length - 1];
    }
    function _s(s,r,d){
        if(typeof(s)!=='string'){
            return addext(s);
        }
        if(ie){
            return addext(_ie(s,r,d));
        }else{
            if(typeof(r)=='undefined'){
                return addext(d.querySelector(s));
            }else{
                return addext(d.querySelectorAll(s));
            }
        }
    }
    var ext={
        rcss:function(a,b){
            if(a==''){
                var s=this.className.split(/\s+/) || [];
                for(var i=0,l=s.length;i<l;i++){
                    if(b==s[i]){
                        return this;
                    }
                }
                s.push(b);
                this.className = s.join(' ').trim();
            }else{
                var s=this.className.split(/\s+/) || [];
                for(var i=0,l=s.length;i<l;i++){
                    if(a==s[i]){
                        s[i]=b;
                        break;
                    }
                }
                s=s.join(' ').trim();
                if(s!=this.className){
                    this.className = s;
                }
            }
            return this;
        },
        del:function(){
            this.parentNode.removeChild(this);
        },
        each:function(fn){
            if(this.forEach){
                return this.forEach(fn);
            }else if(this.length){
                for(var i=0,l=this.length;i<l;i++){
                    fn(this[i],i,this);
                }
            }
            return this;
        },
        prev:function(n){
            var n=n||1,el=this;
            if(typeof(n)=='number'){
                while(n>0 && el){
                    el=el.previousElementSibling || el.previousSibling;
                    n--;
                }
            }else{
                n=n.toUpperCase();
                while(el){
                    el=el.previousElementSibling || el.previousSibling;
                    if(el && el.tagName.toUpperCase()==n){
                        break;
                    }
                }
            }
            return addext(el);
        },
        next:function(n){
            var n=n||1,el=this;
            if(typeof(n)=='number'){
                while(n>0 && el){
                    el=el.nextElementSibling || el.nextSibling;
                    n--;
                }
            }else{
                n=n.toUpperCase();
                while(el){
                    el=el.nextElementSibling || el.nextSibling;
                    if(el && el.tagName.toUpperCase()==n){
                        break;
                    }
                }
            }
            return addext(el);
        },
        parent:function(n){
            var n=n||1,el=this;
            if(typeof(n)=='number'){
                while(n>0 && el){
                    el=el.parentNode;
                    n--;
                }
            }else{
                n=n.toUpperCase();
                while(el){
                    el=el.parentNode;
                    if(el && el.tagName.toUpperCase()==n){
                        break;
                    }
                }
            }
            return addext(el);
        },
        child:function(n){
            var m;
            if(typeof(n)=='undefined'){
                m=this.children;
            }else{
                m=this.children[n];
            }
            return addext(m);
        },
        use:function(f){
            f.apply(this);
            return this;
        },
        offset:function(){
            return this.getBoundingClientRect();
        },
        $:function(s,r){
            return _s(s,r,this);
        },
        css:function(obj){
            for(var i in obj){
                this.style[i]=obj[i];
            }
            return this;
        },
        show:function(f,t,v){
            var t=t||0.2,v=v||1,self=this,cs=ex.fix('transition','all '+t+'s linear');
            if(!this.$){
                addext(this);
            }
            this.style.opacity=0.01;
            if(this.style.display=='none'){
                this.style.display='';
            }else{
                this.style.display='block';
            }
            cs.opacity=v;
            setTimeout(function(){
                self.css(cs);
            },15);
            if(f){
                setTimeout(function(){
                    f.call(self);
                },t);
            }
        },
        hide:function(f,t){
            var t=t||0.2,self=this;
            if(!this.$){
                addext(this);
            }
            this.css(ex.fix('transition','all '+t+'s linear'));
            setTimeout(function(){
                self.style.opacity=0.01;
            },1);
            setTimeout(function(){
                self.style.display='none';
                if(f){
                    f.call(self);
                }
            },t*1000);
        }
    };

    function addext(obj){
        var arf=function(i){
            return function(){
                var r=[],fn=arguments.callee,arg=arguments;
                this.each(function(v,k){
                    if(!v)return;
                    if(v.length && typeof(v.tagName)=='undefined'){
                        r.push(fn.apply(v,arg));
                    }else{
                        v.index=k;
                        r.push(ext[i].apply(v,arg));
                    }
                });
                return addext(r);
            }
        };
        if(obj && !obj.$){
            for(var i in ext){
                if(obj.length && i!='each' && typeof(obj.tagName)=='undefined'){
                    obj[i]=arf(i);
                }else{
                    obj[i]=ext[i];
                }
            }
        }
        return obj;
    }
    function ex(s,r){
        return _s(s,r,doc);
    }
    ex.$$=function(a){
        if(a.indexOf('<')==-1){
            return doc.createElement(a);
        }else{
            var r=doc.createElement('div');
            r.innerHTML=a;
            if(r.children.length == 1){
                return addext(r.children[0]);
            }
            return addext(r.children);
        }
    };
    ex.rand=function(n,m,r){
        if(r){
            return Math.random()*(m-n)+n;
        }else{
            return Math.round(Math.random()*(m-n)+n);
        }
    };
    ex.fix=function(s,v){
        var ar=['Moz','Ms','O','Webkit'],r={},f=function(a,b){return b.toUpperCase();},
            ds=s.replace(/([a-z]{1})/,f).replace(/(-[a-z]{1})/g,f);
        for(var i=0;i<ar.length;i++){
            r[ar[i]+ds]=v;
        }
        r[s]=v;
        return r;
    };
    ex.on=function(el,ev,fn){
        if(!el)return;
        if(el.attachEvent){
            el.attachEvent("on"+ev,function(){
                fn.apply(el,arguments)
            });
        }else{
            el.addEventListener(ev,fn,false)
        }
    };
    var ajone=[];
    ex.aj=function(url,data,f,gs){
        var self=this,gs,key;
        var k=url.replace(/[^\w]/g,'');
        if(typeof(ajone[k])=='undefined'){
            ajone[k]=true;
            if(window.XMLHttpRequest){
                var xm=new XMLHttpRequest()
            }else{
                var M=['MSXML2.XMLHTTP','Microsoft.XMLHTTP','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0'];
                for(n=0;n<M.length;n++){
                    try{
                        var xm=new ActiveXObject(M[n]);
                        break;
                    }catch(e){};
                }
            };
            xm.open("post",url,true);
            xm.setRequestHeader("is_ajax","1");
            xm.onreadystatechange=function(){
                if(xm.readyState==4){
                    if(xm.status==200){
                        delete ajone[k];
                        if(f){
                            if(gs && gs=='json'){
                                eval('var _j='+xm.responseText);
                                eval(f(_j))
                            }else{
                                eval(f(xm.responseText));
                            }
                        }
                    }else if(xm.status==404 || xm.status==403 || xm.status==500){
                        delete ajone[k];
                        if(f){
                            f({'err':1,'msg':'code:'+xm.status});
                        }
                    }
                }
            };
            xm.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
            xm.send(data)
        }
    };
    ex.ldimg=function (src,f){
        var img=new Image();
        img.onerror=function(){f(false)};
        img.src=src;
        img.onload=function(){if(img.complete==true){f(img)}};
    };
    var _ag=0;
    function aimg(fm,el){
        var a=el.getAttribute('fname'),
            fn=el.getAttribute('funbak'),
            sn=el.getAttribute('funstr') || false,
            d=el.getAttribute('furl') || fm.getAttribute('action'),
            el=ex(el),
            w=el.offsetWidth,
            h=el.offsetHeight,
            f=ex.$$('<div style="opacity:0;top:0;left:0px;position:absolute;z-index:99;filter:alpha(opacity=0);width:'+w+'px;height:'+h+'px;"><form method="post" enctype="multipart/form-data" action="'+d+'" target="'+a+_ag+'i"><input type="file" name="'+a+'" style="cursor:pointer; width:'+w+'px;height:'+h+'px;" /></form><iframe name="'+a+_ag+'i" id="'+a+_ag+'i" style="display:none"></iframe></div>');
        doc.body.appendChild(f);
        var ba=f.children[0].children[0],bc=f.children[1],bd=win.frames[a+_ag+'i'];
        ba.onchange=function(){
            if(sn){
                eval('sn='+sn);
                if(sn.call(this)){
                    this.form.submit();
                }
            }else{
                this.form.submit();
            }
        };
        ex.on(el,'mouseover',function(){
            var ow=this.offset();
            f.css({'top':ow.top+Math.max(doc.body.scrollTop,doc.documentElement.scrollTop)+'px','left':ow.left+Math.max(doc.body.scrollLeft,doc.documentElement.scrollLeft)+'px'});
        });
        ex.on(bc,'load',function(){
            var s=bd.document.body.innerHTML;
            if(s){
                eval('s='+s+';fn='+fn);
                bd.document.body.innerHTML='';
                fn.call(el,s);
            }
        });
        _ag++;
    }
    ex.F=function(){
        var ts='',css='F_css',
            tsf=function(a){
                var a=ex(a);
                if(typeof(a.getAttribute('ts'))=='string'){
                    return a.getAttribute('ts')+'<br>';
                }else if(a.getAttribute('placeholder')){
                    return a.getAttribute('placeholder')+'<br>';
                }else if(a.prev()){
                    return a.prev().innerHTML+'涓嶈兘涓虹┖<br>';
                }else{
                    return a.parent().prev().innerHTML+'涓嶈兘涓虹┖<br>';
                }
            },
            st1=function(em){
                eval('var re = new RegExp(/'+em.getAttribute('exp')+'/)');
                var em=ex(em);
                if(!re.test(em.value)){
                    em.rcss('',css);
                    ts+=tsf(em)
                }else{
                    em.rcss(css,'');
                }
            },
            st2=function(em,ep){
                var v=true,em=ex(em);
                if(em.type=='checkbox' && em.checked==false){
                    ts+='璇烽€夋嫨'+tsf(em)+'锛�<br>';
                    v=false;
                }else if(em.value.trim()==''){
                    ts+=tsf(em);
                    v=false;
                }
                if(v===false){
                    em.rcss('',css);
                }else{
                    em.rcss(css,'');
                }
                if(ep && v){
                    st1(em);
                }
            },
            st=function(){
                var fs=doc.forms;
                for(var a=0,l=fs.length;a<l;a++){
                    for(var b=0,j=fs[a].length;b<j;b++){
                        if(typeof(fs[a][b].f)!='undefined'){
                            continue;
                        }
                        fs[a][b].f=true;
                        if(typeof(fs[a][b].getAttribute('req'))=='string'){
                            if(typeof(fs[a][b].getAttribute('exp'))=='string'){
                                ex.on(fs[a][b],'blur',function(){st2(this,true)});
                            }else{
                                ex.on(fs[a][b],'blur',function(){st2(this,false)});
                            }
                        }else if(typeof(fs[a][b].getAttribute('exp'))=='string'){
                            ex.on(fs[a][b],'blur',function(){
                                if(this.value!=''){
                                    st1(this);
                                }else{
                                    ex(this).rcss(css,'');
                                }
                            })
                        }
                        if(typeof(fs[a][b].getAttribute('fname'))=='string'){
                            aimg(fs[a],fs[a][b])
                        }
                    }
                    if(typeof(fs[a].f)!='undefined'){
                        continue;
                    }
                    fs[a].f=false;
                    ex.on(fs[a],'submit',function(e){
                        ts='';
                        for(var i=0,j=this.length;i<j;i++){
                            if(typeof(this[i].getAttribute('req'))=='string'){
                                if(this[i].type=='radio'){
                                    var rs=this[this[i].name],z=true;
                                    for(var r=0;r<rs.length;r++){
                                        if(rs[r].checked){
                                            z=false;
                                            break;
                                        }
                                    }
                                    if(z){
                                        ex(rs[0]).rcss('',css);
                                        ts+=tsf(this[i]);
                                    }
                                    i+=(rs.length-1);
                                }else if(typeof(this[i].getAttribute('exp'))=='string'){
                                    st2(this[i],true);
                                }else{
                                    st2(this[i],false);
                                }
                            }else if(typeof(this[i].getAttribute('exp'))=='string'){
                                if(this[i].value!=''){
                                    st1(this[i]);
                                }else{
                                    ex(this[i]).rcss(css,'');
                                }
                            }
                        }
                        if(ts){
                            ex.alert(ts.replace(/锛�/g,''));
                            try{
                                e.preventDefault();
                            }catch(o){
                                e.returnValue=false;
                            };
                        }else{
                            this.f=true;
                            if(this.fun){
                                this.fun()
                            }
                        }
                    });
                    if(typeof(fs[a].getAttribute('onsubmit'))!='undefined'){
                        fs[a].fun=fs[a].onsubmit;
                        fs[a].onsubmit='';
                    }
                }
                return true;
            };
        return st()
    };

    ex.urlencode=function(s){
        return encodeURIComponent(s);
    };

    ex.fnr=function(f){
        var s=[];
        for(var i=0,l=f.length;i<l;i++){
            if(f[i].name&&f[i].name!=''){
                switch(f[i].tagName.toLowerCase()){
                    case'select':for(var a=0;a<f[i].length;a++){
                        if(f[i][a].selected){
                            s.push(f[i].name+'='+ex.urlencode(f[i][a].value))
                        }
                    };
                        break;
                    default:switch(f[i].type.toLowerCase()){
                        case'radio':var fo=f[f[i].name];
                            if(!fo.length){
                                fo=[fo];
                            }
                            for(var a=0;a<fo.length;a++){
                                if(fo[a].checked){
                                    s.push(fo[a].name+'='+ex.urlencode(fo[a].value));
                                    break
                                }
                            };
                            i+=(fo.length-1);
                            break;
                        case'checkbox':if(f[i].checked){
                            s.push(f[i].name+'='+ex.urlencode(f[i].value))
                        };
                            break;
                        default:s.push(f[i].name+'='+ex.urlencode(f[i].value));
                            break
                    }
                }
            }
        };
        return s.join('&')
    };

    function div(o){
        if(o.bg){
            var bg=ex.$$('<div class="G_bg"></div>');
        }
        if(o.css=='G_confirm'){
            var div=ex.$$('<div class="'+o.css+'"><p class="G_p1">'+o.s+'</p><p class="G_p2"><span class="G_span">纭</span><span class="G_span">鍙栨秷</span></p></div>');
            div.children[1].children[0].onclick=function(){
                hd(true)
            };
            div.children[1].children[1].onclick=function(){
                hd(false);
            }
        }else if(o.css=='G_prompt'){
            var div=ex.$$('<div class="'+o.css+'"><p class="G_p1">'+o.s+'</p><p class="G_p3"><input type="text" /></p><p class="G_p2"><span class="G_span">确定</span><span class="G_span">取消</span></p></div>');
            div.children[2].children[0].onclick=function(){
                hd(div.children[1].children[0].value)
            };
            div.children[2].children[1].onclick=function(){
                hd(false);
            }
        }else if(o.css=='G_box'){
            var _h = '';
            if(o.t){
                _h = '<div class="'+o.css+'"><div class="G_title">'+o.t+'</div><div class="G_content">'+o.s+'</div><span class="G_close">ⅹ</span></div>';
            }else{
                _h = '<div class="G_box">'+o.s+'</div>';
            }
            var div=ex.$$(_h);
            if(o.t){
                ex.move(div.children[0]);
                div.children[2].onclick=function(){
                    hd(false)
                };
                this.close=function(){
                    div.children[2].onclick();
                };
            }else{
                if(o.ac){
                    div.onclick=function(){
                        hd(false)
                    };
                }
                this.close=function(){
                    div.onclick();
                };
            }
        }else{
            var div=ex.$$('<div class="'+o.css+'"><p class="G_msg">'+o.s+'</p></div>');
            setTimeout(function(){
                hd(true)
            },o.t);
        }


        if(o.bg){
            doc.body.appendChild(bg);
            bg.show(false,0.2,0.6);
        }
        doc.body.appendChild(div);

        var h=doc.documentElement.clientHeight;
        if(o.css=='G_box' && h-20 < div.offsetHeight){
              div.children[1].style.height=h-20-div.children[0].offsetHeight+'px';
              div.children[1].style.overflowX='hidden';
              div.children[1].style.overflowY='auto';
        }

        function ct(){
            var lt=div.offsetWidth/2 , tp=div.offsetHeight/2;
               div.style.left='50%';
               div.style.top='50%';
               div.style.marginLeft='-'+lt+'px';
               div.style.marginTop='-'+tp+'px';
        }
        function hd(v){
            function d(){
                this.del();
            }
            if(o.bg){
                bg.hide(d);
            }
            div.hide(d);
            if(o.f){
                o.f(v);
            }
        }
        this.center = function(){
            ct();
        }
        ct();
        div.show();
        if(o.css=='G_prompt'){
            div.children[2].children[0].focus();
        }
    }
    var mt=3000;
    ex.notice=function(s,b,f,t){
        var t=t||mt;
        new div({'s':s,'bg':b,'t':t,'f':f,'css':'G_notice'})
    };
    ex.alert=function(s,b,f,t){
        var t=t||mt;
        new div({'s':s,'bg':b,'t':t,'f':f,'css':'G_alert'})
    };
    ex.success=function(s,b,f,t){
        var t=t||mt;
        new div({'s':s,'bg':b,'t':t,'f':f,'css':'G_success'})
    };
    ex.error=function(s,b,f,t){
        var t=t||mt;
        new div({'s':s,'bg':b,'t':t,'f':f,'css':'G_error'})
    };

    ex.confirm=function(s,f,b){
        new div({'s':s,'bg':b,'f':f,'css':'G_confirm'})
    };
    ex.prompt=function(s,f,b){
        new div({'s':s,'bg':b,'f':f,'css':'G_prompt'})
    };

    ex.box=function(t,s,b,f,c){
        return new div({'s':s,'bg':b,'t':t,'f':f,'css':'G_box','ac':c})
    };
    ex.move=function(ob,oa){

        function a(ob,oa){
            ob.onmousedown=function(e){
                var e=e||event,w=oa.offset(),
                    x=e.clientX-w.left,
                    t=Math.max(doc.body.scrollTop,doc.documentElement.scrollTop),
                    y=e.clientY-w.top,
                    co=ex.fix('transition','none');
                co.position='absolute';
                co.left=w.left+'px';
                co.top=w.top+'px';
                co.margin=0;
                ex(oa).css(co);
                try{
                    e.preventDefault();
                }catch(o){
                    e.returnValue=false;
                };
                doc.body.appendChild(oa);
                doc.onmousemove=function(e){
                    var e=e||event;
                    oa.style.top=e.clientY+t-y+'px';
                    oa.style.left=e.clientX-x+'px';
                };
            };
            ob.onmouseup=function(){
                doc.onmousemove='';
            };
        };
        ob=ex(ob);
        new a(ob,oa||ob.parent());
    };
    ex.tppl = function (tpl, data, fast){
        var fn =  function (d, f) {
            if(f||fast){
                fn.$$ = fn.$$ || new Function(fn.$);
                return fn.$$.apply(d);
            }else{
                var i, k = [], v = [];
                for (i in d) {
                    k.push(i);
                    v.push(d[i]);
                }
                return (new Function(k, fn.$)).apply(d, v);
            }
        };
        if(!fn.$){
            fn.$ = 'var $="";';
            var tpls = tpl.replace(/[\r\t\n]/g, " ").split('[:')
                , i = 0
            while(i<tpls.length){
                var p = tpls[i];
                if(i){
                    var x = p.indexOf(':]');
                    fn.$ += p.substr(0, x);
                    p = p.substr(x+2)
                }
                fn.$ += "$+='"+p.replace(/\'/g,"\\'").replace(/\[\=\:(.*?)\:\]/g, "'+$1+'")+"';";
                i++;
            }
            fn.$ += "return $";
        }

        return data ? fn(data) : fn;
    }
    win.G=ex;
})(window,document);
